package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserJudges;
import com.ruoyi.system.domain.SysUsers;
import com.ruoyi.system.domain.SyseEventsPosted;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.mapper.SysEventsPostedMapper;
import com.ruoyi.system.mapper.SysUserJudgesMapper;
import com.ruoyi.system.mapper.SysUsersMapper;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.service.ISysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SysUsersServiceImpl implements ISysUsersService{
    @Autowired
    private SysUsersMapper usersMapper;

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysEventsPostedMapper sysEventsPostedMapper;

    @Autowired
    private SysUserJudgesMapper sysUserJudgesMapper;

    @Override
    public SysUsers queryUserByTid(SysUsers sysUsers) {
        return usersMapper.queryUserByTid(sysUsers);
    }

    @Override
    public List<SysUserJudges> readUserAll(Integer epid,String nickName, String epschool,Integer pageNum,Integer pageSize) {
        Map<String, Object> param = new HashMap<>();
        param.put("epid",epid);
        param.put("nickName",nickName);
        param.put("epschool",epschool);
        PageHelper.startPage(pageNum,pageSize);
        return usersMapper.readUserAll(param);
    }

    @Override
    public long updateStatus(Integer userId) {
        return usersMapper.updateStatus(userId);
    }

    @Override
    public Map<String, Object> readEventAll(SyseEventsPosted syseEventsPosted) {
        Map<String,Object> map = new HashMap<>();
        map.put("readEventAll", usersMapper.readEventAll(syseEventsPosted));
        map.put("total", usersMapper.EventAllCount());
        return map;
    }


    @Override
    public List<SysUsers> readNoUser(SysUsers sysUsers,Integer pageNum,Integer pageSize) {
        //判断赛事id是否为空
        if(StringUtils.isNotNull(sysUsers.getEpid())){
            //1.根据赛事id查询赛事信息
            SyseEventsPosted syseEventsPosted = sysEventsPostedMapper.selectById(sysUsers.getEpid().intValue());
            //2.查询该赛事下的所有 已经被邀请的评委和邀请中的评委
            List<String> uidList = new ArrayList<>();
            if(StringUtils.isNotNull(syseEventsPosted) && StringUtils.isNotNull(syseEventsPosted.getEptid())){
                //根据赛事id 和 邀请状态为 1接受  2邀请中  这些人不应该出现再被邀请的列表中
                List<SysUserJudges> sysUserJudgesYes = sysUserJudgesMapper.selectByEpidAndStatus(syseEventsPosted.getEpid());
                //获取到已经出现再该赛事的用户
                for(SysUserJudges s:sysUserJudgesYes){
                    uidList.add(s.getUid());
                }
                //如果该赛事下 被邀请的用户为空 则查询全部是评委的用户
                PageHelper.startPage(pageNum,pageSize);
                Map<String, Object> paramLike = new HashMap<>();
                paramLike.put("nickName",sysUsers.getNickName()); //用户名
                paramLike.put("phoneNumber",sysUsers.getPhoneNumber()); //用户名
                if(!CollectionUtils.isEmpty(uidList)){
                    paramLike.put("userIds",uidList);  //已经报名该赛事人的id
                }
                return usersMapper.selectUserCondition(paramLike);
            }
        }
        return new ArrayList<SysUsers>();
    }

    @Override
    public List<Users> readNoUserForSchool(SysUsers sysUsers, String epschool, Integer pageNum, Integer pageSize) {
        List<Users> usersList = new ArrayList<>();
        //判断赛事id是否为空
        if(StringUtils.isNotNull(sysUsers.getEpid())){
            //1.根据赛事id查询赛事信息
//            SyseEventsPosted eventsPosteed = sysEventsPostedMapper.selectById(sysUsers.getEpid().intValue());
            //2. 根据赛事id 和 邀请状态为 1接受  2邀请中  这些人不应该出现再被邀请的列表中
            List<SysUserJudges> sysUserJudgesYes = sysUserJudgesMapper.selectByEpidAndStatus(sysUsers.getEpid().intValue());
            //3. mongodb中查询该学校的所有老师
            List<Users> teachers = userDao.queryTeachers(epschool, sysUsers.getNickName(), sysUsers.getPhoneNumber());
            boolean flag = false;
            for (Users teacher : teachers) {
                for (SysUserJudges sysUserJudgesYe : sysUserJudgesYes) {
                    //如果当前老师账号已被邀请评委
                    if (teacher.getId().equals(sysUserJudgesYe.getUid())) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    usersList.add(teacher);
                }
            }
        }
        return usersList;
    }

    @Override
    public SysUsers readUserById(Integer userId) {
        return usersMapper.readUserById(userId);
    }

    @Override
    public long insertUser(SysUsers sysUsers) {
        return usersMapper.insertUser(sysUsers);
    }

    @Override
    public SysUsers readInsertUser(Integer userId) {
        return usersMapper.readInsertUser(userId);
    }

    @Override
    public long insertUserEvent(long userId, String epid) {
        return usersMapper.insertUserEvent(userId,epid);
    }

    @Override
    public long readLastUserId() {
        return usersMapper.readLastUserId();
    }

    @Override
    public long deleteUser(long userId) {
        return usersMapper.deleteUser(userId);
    }


    @Override
    public long addJudges(SysUsers users) {

        SysUserJudges sysUserJudges = usersMapper.queryIsJudges(users);
        if (sysUserJudges!=null){
            return 0;
        }else {
            return  usersMapper.addJudges(users);
        }
    }

    @Override
    public List<SyseEventsPosted> readEvents(String schoolName) {
        return usersMapper.readEvents(schoolName);
    }
}
